package com.ruoyi.ceshi.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.ceshi.domain.Fj;
import com.ruoyi.ceshi.mapper.CeshiMapper;
import com.ruoyi.ceshi.domain.Ceshi;
import com.ruoyi.ceshi.service.ICeshiService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 测试Service业务层处理
 *
 * @author huacai
 * @date 2024-05-07
 */
@Service
public class CeshiServiceImpl implements ICeshiService {
    @Resource
    private CeshiMapper ceshiMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询测试
     *
     * @param ceshiId 测试主键
     * @return 测试
     */
    @Override
    public Ceshi selectCeshiByCeshiId(String ceshiId) {
        return ceshiMapper.selectCeshiByCeshiId(ceshiId);
    }

    /**
     * 查询测试列表
     *
     * @param ceshi 测试
     * @return 测试
     */
    @Override
    public List<Ceshi> selectCeshiList(Ceshi ceshi) {
        return ceshiMapper.selectCeshiList(ceshi);
    }

    /**
     * 新增测试
     *
     * @param ceshi 测试
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCeshi(Ceshi ceshi) {
        ceshi.setName("我有名称了");
        int rows = ceshiMapper.insertCeshi(ceshi);
        insertFj(ceshi);
        return rows;
    }

    /**
     * 批量新增测试
     *
     * @param ceshis 测试List
     * @return 结果
     */
    @Override
    public int batchInsertCeshi(List<Ceshi> ceshis) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(ceshis)) {
            try {
                for (int i = 0; i < ceshis.size(); i++) {
                    int row = ceshiMapper.insertCeshi(ceshis.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == ceshis.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改测试
     *
     * @param ceshi 测试
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCeshi(Ceshi ceshi) {
        ceshiMapper.deleteFjByCeshiId(ceshi.getCeshiId());
        insertFj(ceshi);
        return ceshiMapper.updateCeshi(ceshi);
    }

    /**
     * 批量删除测试
     *
     * @param ceshiIds 需要删除的测试主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCeshiByCeshiIds(String[] ceshiIds) {
        ceshiMapper.deleteFjByCeshiIds(ceshiIds);
        return ceshiMapper.deleteCeshiByCeshiIds(ceshiIds);
    }

    /**
     * 删除测试信息
     *
     * @param ceshiId 测试主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCeshiByCeshiId(String ceshiId) {
        ceshiMapper.deleteFjByCeshiId(ceshiId);
        return ceshiMapper.deleteCeshiByCeshiId(ceshiId);
    }

    @Override
    public int insertLxglXmsbFjByxmsbId(Fj fj) {
        return ceshiMapper.insertLxglXmsbFjByxmsbId(fj);
    }

    @Override
    public List<Fj> selectLxglXmsbFjList(Fj fj) {
        return ceshiMapper.selectLxglXmsbFjList(fj);
    }

    /**
     * 新增测试附件信息
     *
     * @param ceshi 测试对象
     */
    public void insertFj(Ceshi ceshi) {
        List<Fj> fjList = ceshi.getFjList();
        String ceshiId = ceshi.getCeshiId();
        if (StringUtils.isNotNull(fjList)) {
            List<Fj> list = new ArrayList<Fj>();
            for (Fj fj : fjList) {
                fj.setCeshiId(ceshiId);
                list.add(fj);
            }
//            if (list.size() > 0)
//            {
//                ceshiMapper.batchFj(list);
//            }
        }
    }
}
